package com.youlai.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.converter.StudentConverter;
import com.youlai.system.mapper.ExampleStudentMapper;
import com.youlai.system.pojo.entity.ExampleStudent;
import com.youlai.system.pojo.form.StudentForm;
import com.youlai.system.pojo.query.StudentPageQuery;
import com.youlai.system.pojo.vo.StudentPageVO;
import com.youlai.system.service.ExampleStudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ExampleStudentServiceImpl extends ServiceImpl<ExampleStudentMapper, ExampleStudent> implements ExampleStudentService {

    private final StudentConverter studentConverter;

    @Override
    public Page<StudentPageVO> getStudentPage(StudentPageQuery queryParams) {

        //查询参数
        //查询页数
        int pageNum = queryParams.getPageNum();
        //查询数量大小
        int pageSize = queryParams.getPageSize();
        //查询条件
        String keywords = queryParams.getKeywords();

        Page<ExampleStudent> studentItemPage = this.page(
                new Page<>(pageNum, pageSize),
                new LambdaQueryWrapper<ExampleStudent>()
                        /**
                         * .like 模糊查询，这里就是模糊查询学生姓名
                         * .select 就是将查询出来的信息显示出来，这里就是全显示
                         */
                        .like(StrUtil.isNotBlank(keywords), ExampleStudent::getStudentName, keywords)
                        .select(ExampleStudent::getId, ExampleStudent::getStudentName, ExampleStudent::getStudentAge, ExampleStudent::getStudentAddress)
        );

        Page<StudentPageVO> studentPageVOPage = studentConverter.entityToPage(studentItemPage);


        return studentPageVOPage;
    }

    @Override
    public StudentForm getStudentForm(Long id) {

        ExampleStudent exampleStudent = this.getOne(new LambdaQueryWrapper<ExampleStudent>()
                .eq(ExampleStudent::getId, id)
                .select(
                        ExampleStudent::getId,
                        ExampleStudent::getStudentName,
                        ExampleStudent::getStudentAge,
                        ExampleStudent::getStudentAddress

                )
        );
        StudentForm studentForm = studentConverter.entityToForm(exampleStudent);


        return studentForm;
    }

    @Override
    public boolean saveStudent(StudentForm studentForm) {

        ExampleStudent exampleStudent = studentConverter.formToEntity(studentForm);

        boolean result = this.save(exampleStudent);


        return result;
    }

    @Override
    public boolean updateStudent(Long id, StudentForm studentForm) {


        ExampleStudent exampleStudent = studentConverter.formToEntity(studentForm);

        boolean result = this.updateById(exampleStudent);


        return result;
    }

    @Override
    public boolean deleteStudent(String ids) {

        Assert.isTrue(StrUtil.isNotBlank(ids), "删除数据为空");
        //
        List<Long> idsList = Arrays.asList(ids.split(","))
                .stream()
                .map(id -> Long.parseLong(id))
                .collect(Collectors.toList());

        // 删除字典数据项
        boolean result = this.removeByIds(idsList);

        return result;
    }
}
